// 导入登录接口
import { loginApi, getUserinforApi } from '@/api/user'
// 导入本地存储token方法
import { setToken, getToken, delToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    // username: '',
    // 先从本地获取，获取到值就存起来
    token: getToken() || '',
    userInfo: {}
  },
  getters: {
  },
  mutations: {
    // getusername (state, data) {
    //   state.username = data
    // },
    /**
     * 存储token
     * @param {*} state 所以变量
     * @param {*} data
     */
    gettoken (state, data) {
      // 1.内存
      state.token = data
      // 2. 本地
      setToken(data)
    },
    // 删除token
    deltoken (state) {
      state.token = ''
      delToken()
    },
    setuserInfo (state, laydata) {
      state.userInfo = laydata
    }
  },
  actions: {
    /**
     *
     * @param {*} param0 context上下文
     * @param {*} loginForm 页面调用getTokenAction传入的参数：用户民和密码
     */
    async getLogin ({ commit }, loginForm) {
      const { data } = await loginApi(loginForm)
      if (data.code === 0) {
        // commit('getusername', loginForm.username)
        commit('gettoken', data.token)
      }
      return data
    },
    async getUser ({ commit }) {
      const { data } = await getUserinforApi()
      console.log('用户信息', data)
      commit('setuserInfo', data.data)
    }
  }
}
